package org.jeecg.modules.tankerRegistration.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.modules.tankerRegistration.entity.TankerByPlateVO;
import org.jeecg.modules.tankerRegistration.entity.TankerRegistration;
import org.jeecg.modules.tankerRegistration.service.ITankerRegistrationService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/tanker")
@Slf4j
public class TankerController {

    @Autowired
    private ITankerRegistrationService tankerRegistrationService;

    /**
     * 根据车牌号查询最新一条记录
     *
     * @param plate 车牌号
     * @return 最新一条记录
     */
    @AutoLog(value = "金钟-根据车牌号查询最新一条记录")
    @ApiOperation(value = "金钟-根据车牌号查询最新一条记录", notes = "金钟-根据车牌号查询最新一条记录")
    @GetMapping(value = "/getLastByPlate")
    public Result<?> queryPageList(@RequestParam(name = "plate") String plate) {
        LambdaQueryWrapper<TankerRegistration> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(TankerRegistration::getPlateNo, plate);
        wrapper.orderByDesc(TankerRegistration::getCreateTime);
        wrapper.last("limit 1");
        TankerRegistration one = tankerRegistrationService.getOne(wrapper);
        if (one == null){
            return Result.error("未查询到记录");
        }
        TankerByPlateVO tankerByPlateVO = new TankerByPlateVO();
        BeanUtils.copyProperties(one, tankerByPlateVO);
        return Result.OK(tankerByPlateVO);
    }

}
